/* This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <avr/io.h>
#include "defs.h"
#include "i2cmaster.h"


#define TMP_INT_DEV_ADDR 0x90
#define TMP_EXT_DEV_ADDR 0x92

#define TEMP_REG 	0x00

int readTemp(uint8_t devAddr,int16_t *data)
{
    uint8_t lsb = 0;
    uint8_t msb = 0;
    int ret;

    ret = i2c_start(devAddr+I2C_WRITE);       // set device address and write mode
    if ( ret ) {
        /* failed to issue start condition, possibly no device found */
        i2c_stop();        
    }else {
        /* issuing start condition ok, device accessible */
        i2c_write(TEMP_REG);                    // write 0x00 to pointer register (set temperature reg)
        i2c_stop();                            // set stop conditon = release bus

        /* write ok, read value back from eeprom address 0x05, wait until 
           the device is no longer busy from the previous write operation */
        i2c_start_wait(devAddr+I2C_WRITE);     // set device address and write mode
        i2c_write(TEMP_REG);               // write address = 0x0 (Temp reg)
        i2c_rep_start(devAddr+I2C_READ);       // set device address and read mode
        msb = i2c_readAck();                    // read msb byte
        lsb = i2c_readNak();                    // read lsb byte
        i2c_stop();                          // set stop conditon = release bus
    
        *data = (int16_t) msb << 8;
        *data |= (int16_t) (0x00FF & lsb);
    }
     
    return ret;  
}

//Read a tmp102 sensor on a given temp_number or channel
double extTempRead(void)
{
	int16_t temp = 0;
    int retVal;

    retVal = readTemp(TMP_EXT_DEV_ADDR, &temp);
    if (retVal != 0) goto FAIL;

	//Test-
	//msb = 0b11100111;
	//lsb = 0b00000000; //From the datasheet, -25C

	temp >>= 4; //The TMP102 temperature registers are left justified, correctly right justify them
	
	//The tmp102 does twos compliment but has the negative bit in the wrong spot, so test for it and correct if needed
	if(temp & (1<<11))
		temp |= 0xF800; //Set bits 11 to 15 to 1s to get this reading into real twos compliment

	return (((double) temp) * 0.0625);

FAIL:
    return 1.0 * temp;
}

//Read a onboard tcn75 sensor (Which is not mounted on PCB)
double intTempRead(void)
{

	uint8_t msb, lsb;
	int16_t temp = 0;
    int retVal;
   
	//Test
	//msb = 0b00011001;
	//lsb = 0b00000000; //From the datasheet, -25C

    retVal = readTemp(TMP_INT_DEV_ADDR, &temp);
    if (retVal != 0) goto FAIL;

    msb = (uint8_t) (temp >> 8);
    lsb = (uint8_t) (temp & 0xFF);

    temp = 0;

    if (msb & 0x80){
        temp = 0xFE00;
    }

    temp |= msb << 1;	

    /*LSB of the temperature is bit7 of lsb*/
    if (lsb & 0x80){
        temp |= 1;
	}

	return (((double) temp) * 0.5);

FAIL:
    return 1.0 *temp;
}
